package com.aode.music.adapter.home;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.RecyclerView;

import com.aode.common.bean.response.Music;
import com.aode.music.R;
import com.aode.music.databinding.ItemHomeMusicBinding;

import java.util.ArrayList;

public class HomeMusicAdapter extends RecyclerView.Adapter {
    private Context context;
    private ArrayList<Music> musicArrayList;

    public HomeMusicAdapter(Context context, ArrayList<Music> musicArrayList){
        this.context = context;
        this.musicArrayList = musicArrayList;
    }
    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        ItemHomeMusicBinding homeMusicBinding = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()), R.layout.item_home_music,parent,false);
        return new HomeMusicHolder(homeMusicBinding);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        if(holder instanceof  HomeMusicHolder){
            HomeMusicHolder homeMusicHolder = (HomeMusicHolder)holder;
            if(musicArrayList.size()>0&&!musicArrayList.isEmpty()){
                Music music = musicArrayList.get(position);
                homeMusicHolder.getHomeMusicBinding().setMusic(music);
            }
        }


    }

    @Override
    public int getItemCount() {
        return musicArrayList.size();
    }
    class  HomeMusicHolder extends RecyclerView.ViewHolder{
        private ItemHomeMusicBinding homeMusicBinding;
        public HomeMusicHolder(ItemHomeMusicBinding homeMusicBinding) {
            super(homeMusicBinding.getRoot());
            this.homeMusicBinding = homeMusicBinding;
        }

        public ItemHomeMusicBinding getHomeMusicBinding() {
            return homeMusicBinding;
        }

        public void setHomeMusicBinding(ItemHomeMusicBinding homeMusicBinding) {
            this.homeMusicBinding = homeMusicBinding;
        }
    }
}
